package jwtgo

import (
	"errors"
	"ginweb-admin/global"
	"ginweb-admin/model/entity/jwt"
	"gorm.io/gorm"
)

type JwtService struct{}

/**
 * @Author kong
 * @Date 2023/12/27
 * @Description 把旧token拉入黑名单
 */
func (jwtService *JwtService) JsonInBlacklist(jwtList jwt.JwtBlacklist) (err error) {
	err = global.DB.Create(&jwtList).Error
	if err != nil {
		return
	}
	return
}

/**
 * @Author kong
 * @Date 2023/12/27
 * @Description 判断JWT是否在黑名单内部
 */
func (jwtService *JwtService) IsBlacklist(jwtToken string) bool {
	err := global.DB.Where("jwt = ?", jwtToken).First(&jwt.JwtBlacklist{}).Error
	isNotFound := errors.Is(err, gorm.ErrRecordNotFound)
	return !isNotFound
}
